import time
import pytest
import allure
import TestCase

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


@allure.feature('TestCase_13: 播放历史页面, 切换播放正常')
@allure.issue(
    'https://www.tapd.cn/56767367/sparrow/tcase/view/1156767367001043373?url_cache_key=17e362af8b12add5e45c5e468a11c15a')
class TestCaseMusic13:
    """
    预期结果: 正常切换播放，且播放成功
    """

    def setup_class(self):
        self.driver = init_driver()
        # 实例化页面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()

        self.music_choice_page = CommonPage(self.driver).get_music_choice_page()
        self.music_index_page = CommonPage(self.driver).get_music_index_page()
        self.music_player_page = CommonPage(self.driver).get_music_player_page()
        self.music_my_collection_page = CommonPage(self.driver).get_music_my_collection_page()
        self.music_play_history_page = CommonPage(self.driver).get_music_play_history_page()

    def teardown_class(self):
        XiaoMaUtils.close_app('music')
        self.driver.quit()

    @allure.story("step1: 进入首页, 获取播放icon")
    @pytest.mark.run(order=131)
    def test_music_13_1_get_stop_icon(self):
        try:
            # 进入音乐
            self.launcher_index_page.click_app_list()
            self.launcher_app_list_page.click_appname(TestCase.MUSIC)
            if self.music_choice_page.is_display_title():
                self.music_choice_page.click_skip()
                
            self.music_index_page.screenshot_part(self.music_index_page.get_thumb_icon(), "testcase13_icon1")
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)
            file1 = open('Image/testcase13_icon1.png', 'rb').read()
            allure.attach(file1, '未播放时的icon', allure.attachment_type.PNG)

    @allure.story("step2: 播放历史页面的音乐, 再次获取播放icon")
    @pytest.mark.run(order=132)
    def test_music_13_2_get_play_icon(self):
        try:
            # 进入播放历史页面
            self.music_index_page.click_my_music()
            self.music_my_collection_page.click_play_history()

            # 点击播放历史页面的音乐
            self.music_play_history_page.click_list_music_cover(1)

            # 再次获取播放icon
            self.music_index_page.screenshot_part(self.music_index_page.get_thumb_icon(), "testcase13_icon2")

            contrast_res = self.music_index_page.image_contrast("testcase13_icon1.png", "testcase13_icon2.png")

            assert contrast_res > 0
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)
            file1 = open('Image/testcase13_icon2.png', 'rb').read()
            allure.attach(file1, '播放时的icon', allure.attachment_type.PNG)


if __name__ == '__main__':
    pass
